package com.swn.demo.address.controller;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swn.common.util.JSONUtil;
import com.swn.demo.address.service.WeiXinAddressService;


@Controller
@RequestMapping("/weinXinAddress")
public class WinXinAddressController {

	
	private static Logger logger = Logger.getLogger("WinXinAddressController.class");
	
	
	@Autowired
	private WeiXinAddressService weiXinService;
	
	
	/**
	 * 
	* @Title: saveOrUpdateAddress
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @author dongliang
	* @data 2018年8月9日    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "/addressAddOrUpdate",method=RequestMethod.POST)
	@ResponseBody
	public String saveOrUpdateAddress(String jsonStr){
		//System.out.println("111111");
		String result = "";
		try {
			@SuppressWarnings("unchecked")
			Map<String,Object> map = JSONUtil.getMapFromJsonString(jsonStr);
			String id = weiXinService.saveOrUpdateAddress(map);
			if(id!=""&&id!=null){
				result = id+"d";//纯数字传输被微信小程序自动科学计数法 加d转字符串
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return "fail";
		}
		return result;		
	}
	
	
	/**
	 * 
	* @Title: queryAllAddress
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @author dongliang
	* @data 2018年8月9日    参数
	* @return List<Map<String,Object>>    返回类型
	* @throws
	 */
	@RequestMapping("/allAddress")
	@ResponseBody
	public List<Map<String,Object>> queryAllAddress(String user_id){
		List<Map<String, Object>> listMap = null;
		try {
			listMap = weiXinService.queryAllAddress(user_id);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return listMap;
	}
	
	
	@ResponseBody
	@RequestMapping("/deleteAddress")
	public String deleteAddress(String id){
		String result = "fail";
		try {
			int i = weiXinService.deleteAddress(id);
			if(i>0){
				result="success";
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return result;
	}
}
